/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GoogleCalendarManagerEvoOptionsDialog.java
 *
 * Created on 17-gen-2012, 10.31.17
 */

package googlecalendarmanagerevo;

import it.illumedellaragione.googlecalendar.ManageConfig;
import javax.swing.JFrame;
import org.jdesktop.application.Action;

/**
 *
 * @author davide.zanetti
 */
public class GoogleCalendarManagerEvoOptionsDialog extends javax.swing.JDialog {

    JFrame mainFrame;
    final String PATH_FILE_CONFIG = "config.properties";

    /** Creates new form GoogleCalendarManagerEvoOptionsDialog */
    public GoogleCalendarManagerEvoOptionsDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    GoogleCalendarManagerEvoOptionsDialog(JFrame mainFrame, boolean modal) {
        super(mainFrame, modal);
        this.mainFrame = mainFrame;
        initComponents();
        readConfig();
        getRootPane().setDefaultButton(saveButton);
    }

    private void readConfig(){
        jTxtUser.setText(ManageConfig.getString(PATH_FILE_CONFIG, "user"));
        jTxtPassword.setText(ManageConfig.getString(PATH_FILE_CONFIG, "pwd"));
        String tAutoconnection = ManageConfig.getString(PATH_FILE_CONFIG, "autoconnection");
        if (tAutoconnection != null) jCheckBoxAutoConnection.setSelected(tAutoconnection.equalsIgnoreCase("true")) ;
        jTxtOffsetUTC.setText(ManageConfig.getString(PATH_FILE_CONFIG, "offsetUTC"));
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jCheckBoxAutoConnection = new javax.swing.JCheckBox();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jTxtUser = new javax.swing.JTextField();
        jTxtPassword = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTxtOffsetUTC = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(googlecalendarmanagerevo.GoogleCalendarManagerEvoApp.class).getContext().getResourceMap(GoogleCalendarManagerEvoOptionsDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jCheckBoxAutoConnection.setText(resourceMap.getString("jCheckBoxAutoConnection.text")); // NOI18N
        jCheckBoxAutoConnection.setName("jCheckBoxAutoConnection"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(googlecalendarmanagerevo.GoogleCalendarManagerEvoApp.class).getContext().getActionMap(GoogleCalendarManagerEvoOptionsDialog.class, this);
        saveButton.setAction(actionMap.get("saveConfig")); // NOI18N
        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N

        cancelButton.setAction(actionMap.get("closeDialog")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        jTxtUser.setText(resourceMap.getString("jTxtUser.text")); // NOI18N
        jTxtUser.setName("jTxtUser"); // NOI18N

        jTxtPassword.setText(resourceMap.getString("jTxtPassword.text")); // NOI18N
        jTxtPassword.setName("jTxtPassword"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jTxtOffsetUTC.setText(resourceMap.getString("jTxtOffsetUTC.text")); // NOI18N
        jTxtOffsetUTC.setName("jTxtOffsetUTC"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBoxAutoConnection)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTxtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                            .addComponent(jTxtUser, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cancelButton)
                        .addGap(18, 18, 18)
                        .addComponent(saveButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(27, 27, 27)
                        .addComponent(jTxtOffsetUTC, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTxtUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTxtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jCheckBoxAutoConnection)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTxtOffsetUTC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(50, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(164, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void saveConfig() {
        ManageConfig.setProperty(PATH_FILE_CONFIG, "user", jTxtUser.getText().trim());
        ManageConfig.setProperty(PATH_FILE_CONFIG, "pwd", jTxtPassword.getText().trim());
        ManageConfig.setProperty(PATH_FILE_CONFIG, "autoconnection", (jCheckBoxAutoConnection.isSelected()? "true":"false"));
        ManageConfig.setProperty(PATH_FILE_CONFIG, "offsetUTC", jTxtOffsetUTC.getText().trim());
        this.show(false);
    }

    @Action
    public void closeDialog() {
        //this.show(false);
        dispose();
    }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JCheckBox jCheckBoxAutoConnection;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTxtOffsetUTC;
    private javax.swing.JTextField jTxtPassword;
    private javax.swing.JTextField jTxtUser;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

}
